import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fsExtra, * as fse from 'fs-extra';
import path from 'path';

import { initExecuteCommand, runHvigor } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.EmptyAbility]: 1,
  }
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const entryModulePath = project.getModule('entry').moduleRootPath();
  const featureModulePath = project.getModule('application').moduleRootPath();

  // 首次全量编译
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap'
  });
  const result = runHvigor(projectRoot, commands);
  const entryHapExist =
    await fsExtra.pathExists(path.resolve(entryModulePath, 'build/default/outputs/default/entry-default-unsigned.hap'));
  const featureHapExist = (await fse.readdir(path.resolve(featureModulePath, 'build/default/outputs/default')))
    .some(item => item.includes('-unsigned.hap'));
  record.addStep('执行hap编译构建', result);

  await fsExtra.copyFile(path.resolve(featureModulePath, 'src/main/resources/base/media/icon.png'),
    path.resolve(featureModulePath, 'src/main/resources/base/media/icon2.png'));

  const commandsIncrement = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap'
  });
  const resultIncrement = runHvigor(projectRoot, commandsIncrement);

  await fsExtra.copyFile(path.resolve(featureModulePath, 'build/default/outputs/default/application-entry-default-unsigned.hap'),
    path.resolve(featureModulePath, 'build/default/outputs/default/application-entry-default-unsigned.zip'));
  // eslint-disable-next-line max-len
  await compressing.zip.uncompress(path.resolve(featureModulePath, 'build/default/outputs/default/application-entry-default-unsigned.zip'),
    path.resolve(featureModulePath, 'build/default/outputs/default/application-entry-default-unsigned'));
  const incrementHapExist =
    await fsExtra.pathExists(path.resolve(featureModulePath,
      'build/default/outputs/default/application-entry-default-unsigned/resources/base/media/icon2.png'));

  expect(entryHapExist).toBe(true);
  expect(featureHapExist).toBe(true);
  expect(incrementHapExist).toBe(true);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
  expect(resultIncrement.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Incremental_Compilation_hos_api10_Stage_Ets_Open_Update_009')
  .description('增量编译-新增ability进行增量编译校验')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.defaultOpt())
  .testFun(testFun)
  .load();
